package main

import (
	"fmt"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

/**
执行用时：0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗：2.6 MB, 在所有 Go 提交中击败了99.87%的用户
 */
func rotateRight(head *ListNode, k int) *ListNode {

	if k == 0 || head == nil { return head }

	n := 0
	tail := head
	for p := head; p != nil; p = p.Next {
		tail = p
		n++
	}

	k %= n
	if k == 0 { return head }

	p := head
	for i := 0; i < n - k - 1; i++ { p = p.Next }
	tail.Next = head
	head = p.Next
	p.Next = nil

	return head
}

func main() {

	fmt.Println("Hello World!")
}
